<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图2</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        ul {
            list-style: none;
        }
        .carousel {
            position: relative;
            width: 600px;
            height: 360px;
            border: 1px solid #000;
            margin: 100px auto;
        }
        .carousel ul li {
            position: absolute;
            top: 0;
            width: 150px;
            height: 360px;
            transform-style: preserve-3d;
            /* perspective: 5000px; */
            transition: all 1s ease 0s;

        }

        .carousel ul li div {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-size: 600px 360px;
        }
        /* 设置每个 li 标签下面所有 div 的背景图片 */
        .carousel ul li div:nth-child(1) {
            background-image: url(images/pic1.jpg);
            transform: rotateX(0deg) translateZ(180px);
        }
        .carousel ul li div:nth-child(2) {
            background-image: url(images/pic2.jpg);
            transform: rotateX(90deg) translateZ(180px);
        }
        .carousel ul li div:nth-child(3) {
            background-image: url(images/pic3.jpg);
            transform: rotateX(180deg) translateZ(180px);
        }
        .carousel ul li div:nth-child(4) {
            background-image: url(images/pic4.jpg);
            transform: rotateX(270deg) translateZ(180px);
        }

        .btn {
            display: block;
            width: 100px;
            height: 50px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="carousel">
        <ul></ul>
    </div>
    <input type="button" value="下一张" class="btn">
    <script src="JS/jquery-1.12.4.min.js"></script>
    <script>
        // 获取元素
        var carousel = $(".carousel")
        var ul = $('.carousel ul')
    
        // 声明细分的个数（li 标签的数量）
        var len = 4;
        // 延迟事件，单位 秒
        var duration = 1; 
        // 声明延迟事件
        var delay = 0.1;
        // 盒子宽度
        var width = carousel.width()
        // 每个 li 的宽度
        var liWidth = width / len
        // 通过遍历，生成 ul 中的内容
        for (let index = 0; index < len; index++) {
          var li = $("<li><div></div><div></div><div></div><div></div></li>")
          li.css({
            "width": liWidth,
            "left": index * liWidth + "px",
            "transition-delay": index * delay + "s",
            "transition-duration": duration+ 's',
          }).children().css('background-position', - index * liWidth + "px 0")
          ul.append(li)
        }

        var btn = $('.btn')
        var lis = $('.carousel ul li')
        var sem = 0
        // 给事件添加一把锁，用于防抖
        var lock = false

        btn.click(function() {
          // 先判断锁的状态
          if (lock) return
          // 一旦动画开始，就上锁
          lock = true
          sem -= 90

          lis.css({
            "transform": "rotateX("+sem+"deg)"
          })
          // 动画执行完成时，释放锁
          var timeout = (duration + delay * len) * 1000
          setTimeout(function() {
            lock = false
          }, timeout)
        })
    </script>
</body>
</html>